At the beginning, we had an overview about what postfix, infix and prefix are by a review about the construction of a tree. We practiced on drawing a tree in accordance to the simple code to visualize the differences among them: A. prefix: node + left + right B. infix: left + node +right C. postfix: left + right + node The code of the three different types of tree if posted below:
Apart from working hard to figure out the real sequence of output, we can also have an effortless method by drawing flags beside each node. For example, with infix, a little flag can be drawn on the left side of each node and the sequence is shown as we draw a line to link them up smoothly. Also, you can try to flatten the tree to get a result of infix.
This method can also be utilized when we are expecting different outputs changed by different ways to traverse a tree. For example, when we have a tree as:
class Tree:
def__init__(self, cargo, left=None, right=None):
self.cargo= cargo
self.left= left
self.right= right
def__str__(self):
returnstr(self.cargo)
tree = Tree("i",Tree("b"),Tree("c",Tree("s")))def print_tree1(tree):
if tree isNone: returnprint(tree.cargo, end=" ")
print_tree1(tree.left)
print_tree1(tree.right)def print_tree2(tree):
if tree isNone: return
print_tree2(tree.left)print(tree.cargo, end=" ")
print_tree2(tree.right)def print_tree3(tree):
if tree isNone: return
print_tree3(tree.left)
print_tree3(tree.right)print(tree.cargo, end=" ")
print_tree1(tree)
print_tree2(tree)
print_tree3(tree)
With prefix Tree1, we get: i b c s
With infix Tree2, we get: b i s c
With postfix Tree3, we get: b s c i
As we proceed on understanding prefix, infix and postfix well, we start to learn about the RPN calculator. First of all, we made a comparison between an ordinary calculator and a RPN calculator. It turned out that a RPN calculator need the user to press less buttons and therefore is more convenient and time-efficient to use. Meanwhile, as there is no ambiguous interpretation about the existence of parenthesis and the sequence of calculation in RPN, it is widely used in lots of circumstances where precise calculations are required.
With an ordinary calculator using infix and a RPN calculator using postfix, samples ra given here:
Infix
2 * 3 + 5
2 * (3 + 5)
Postfix
2 3 * 5 +
2 3 5 + *
What actually happens here is that a stack is used in RPN: when a number is appended to the stack, it will accumulate in there, but when a notation such as “+” or “*” is appended, the last two numbers will be “popped” and be calculated. With this logic, a sample code is provided here:
importmathdef postfix_evaluation(s):
s = s.split()
n =len(s)
stack =[]for i inrange(n):
if s[i].isdigit():
stack.append(int(s[i]))elif s[i]=="+":
a = stack.pop()
b = stack.pop()
stack.append(int(a)+int(b))elif s[i]=="*":
a = stack.pop()
b = stack.pop()
stack.append(int(a)*int(b))elif s[i]=="-":
a = stack.pop()
b = stack.pop()
stack.append(int(a) - int(b))elif s[i]=="/":
a = stack.pop()
b = stack.pop()
stack.append(int(a)/int(b))elif s[i]=="%":
a = stack.pop()
b = stack.pop()
stack.append(int(a) % int(b)return stack.pop()
s ="10 2 8 * + 3 -"
val = postfix_evaluation(s)print(val)
With the help of it, we can create a calculator with more functions, such as:
various methods including “-” “/ ” “%” “sin” “cos” etc.
change the postfix into a normal look by printing out the infix version of it
At the beginning, we had an overview about what postfix, infix and prefix are by a review about the construction of a tree.
We practiced on drawing a tree in accordance to the simple code to visualize the differences among them:
A. prefix: node + left + right
B. infix: left + node +right
C. postfix: left + right + node
The code of the three different types of tree if posted below:
Apart from working hard to figure out the real sequence of output, we can also have an effortless method by drawing flags beside each node. For example, with infix, a little flag can be drawn on the left side of each node and the sequence is shown as we draw a line to link them up smoothly. Also, you can try to flatten the tree to get a result of infix.
This method can also be utilized when we are expecting different outputs changed by different ways to traverse a tree. For example, when we have a tree as:
As we proceed on understanding prefix, infix and postfix well, we start to learn about the RPN calculator.
First of all, we made a comparison between an ordinary calculator and a RPN calculator. It turned out that a RPN calculator need the user to press less buttons and therefore is more convenient and time-efficient to use. Meanwhile, as there is no ambiguous interpretation about the existence of parenthesis and the sequence of calculation in RPN, it is widely used in lots of circumstances where precise calculations are required.
With an ordinary calculator using infix and a RPN calculator using postfix, samples ra given here:
What actually happens here is that a stack is used in RPN: when a number is appended to the stack, it will accumulate in there, but when a notation such as “+” or “*” is appended, the last two numbers will be “popped” and be calculated. With this logic, a sample code is provided here:
With the help of it, we can create a calculator with more functions, such as: